package com.XinYun.Library.utils;

import cn.hutool.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class JsonUtil {
    /**
     * 将纯键值对形式的json转换为Map
     * @param jsonObj
     * @return
     */
    public static Map<String,String> toMap(JSONObject jsonObj){
        //是否为单层Json
        if(!jsonObj.toString().matches("\\{.*?\\}")){
            //待返回Map
            HashMap<String, String> strMap = new HashMap<>();
            Set<Map.Entry<String, Object>> entries = jsonObj.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                String key = entry.getKey();
                String value = (String) entry.getValue();
                strMap.put(key,value);
            }
            return strMap;
        }
        throw new RuntimeException("错误的使用方式");
    }
}
